package com.zlm.mall.publisher.service.impl;

import com.zlm.mall.publisher.mapper.OrderWideMapper;
import com.zlm.mall.publisher.service.OrderWideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Author: Harbour
 * Date: 2021-04-23 19:56
 * Desc:
 */
@Service
public class OrderWideServiceImpl implements OrderWideService {

    @Autowired
    OrderWideMapper orderWideMapper;

    @Override
    public BigDecimal selectOrderAmountTotal(String date) {
        return orderWideMapper.selectOrderAmountTotal(date);
    }

    @Override
    public Map<String, BigDecimal> selectOrderAmountHourMap(String date) {

        List<Map> mapList = orderWideMapper.selectOrderAmountHourMap(date);

        Map<String,BigDecimal> orderAmountHourMap = new HashMap();
        for (Map map : mapList) {
            orderAmountHourMap.put(
                String.format("%02d", map.get("hour")),
                (BigDecimal)map.get("amount")
            );
        }
        return orderAmountHourMap;

    }

    @Override
    public List<Map<String, Object>> selectOrderAmountMonthMap(int latestN) {
        List<Map> mapList = orderWideMapper.selectOrderAmountMonthMap();
        List<Map<String, Object>> res = new ArrayList<>();

        int size = mapList.size();
        latestN = latestN == -1 ? size : latestN; // -1时，取得所有数据

        for (int i = size - latestN; i < size; i++) {

            Map<String, Object> orderAmountMonthMap = new HashMap<>();
            Map map = mapList.get(i);

            String month = latestN == size ?
                    map.get("year") + "年" + map.get("month") + "月" : map.get("month") + "月";
            orderAmountMonthMap.put(
                    "x", month
            );
            orderAmountMonthMap.put(
                    "y",
                    map.get("amount")
            );
            res.add(orderAmountMonthMap);
        }
        return res;
    }

    @Override
    public List<Map<String, Object>> selectOrderAmountProvinceMap(int year) {
        List<Map> mapList = orderWideMapper.selectOrderAmountProvinceMap(year);
        List<Map<String, Object>> res = new ArrayList<>();

        for (Map map : mapList) {
            Map<String, Object> subMap = new HashMap<>();
            subMap.put("x",map.get("name"));
            subMap.put("y",map.get("amount"));
            subMap.put("s","thermal");
            res.add(subMap);
        }
        return res;
    }

    @Override
    public List<Map<String, Object>> selectHotTradeMarkMap(int topN) {
        List<Map> mapList = orderWideMapper.selectHotTradeMarkMap(topN);
        List<Map<String, Object>> res = new ArrayList<>();

        int size = mapList.size();
        topN = topN == -1 ? size : topN; // -1时，取得所有数据

        Double totalCount = 0D;
        for (Map map : mapList) {

            String s = ((BigInteger) map.get("hotDegrees")).toString();
            Double count = Double.parseDouble(s);
            map.put("hotDegrees", count);
            totalCount += count;
        }

        for (int i = size - topN; i < size; i++) {
            Map<String, Object> hotTrademarkMap = new HashMap<>();
            Map map = mapList.get(i);

            hotTrademarkMap.put("name", map.get("trademark"));
            hotTrademarkMap.put("value", Math.round(((Double)map.get("hotDegrees"))*100 / totalCount));
            res.add(hotTrademarkMap);
        }

        return res;
    }

    @Override
    public List<Map<String, Object>> selectOrderAmountComparisonMap() {
        List<Map> mapList = orderWideMapper.selectOrderAmountMonthMap();
        List<Map<String, Object>> res = new ArrayList<>();

        String monthStr = new SimpleDateFormat("MM").format(new Date());
        String yearStr = new SimpleDateFormat("yyyy").format(new Date());

        int startMonth = 1;
        int endMonth = Integer.parseInt(monthStr);

        for (Map map : mapList) {
            int month = (int) map.get("month");
            if (month >= startMonth && month <= endMonth) {
                Map<String, Object> subMap = new HashMap<>();
                subMap.put("x", map.get("month") + "月");
                subMap.put("y", map.get("amount"));
                String s = ((int) map.get("year")) == Integer.parseInt(yearStr) ? "s1" : "s2";
                subMap.put("s", s);
                res.add(subMap);
            }
        }
        return res;
    }

    @Override
    public List<Map<String, Object>> selectOrderAmountWeekMap() {
        List<Map> mapList = orderWideMapper.selectOrderAmountWeekMap();
        List<Map<String, Object>> res = new ArrayList<>();

        for (int i=mapList.size()-1; i>=0 ; i--) {
            Map map = mapList.get(i);
            Map<String, Object> orderAmountMonthMap = new HashMap<>();
            String day = ((String) map.get("day")).substring(5);
            orderAmountMonthMap.put(
                    "x",
                    day
            );
            orderAmountMonthMap.put(
                    "y",
                    map.get("amount")
            );
            res.add(orderAmountMonthMap);
        }

        return res;
    }

    @Override
    public List<Map<String, Object>> selectUserAgeGroupMap() {
        List<Map> mapList = orderWideMapper.selectUserAgeGroupMap();
        List<Map<String, Object>> res = new ArrayList<>();

        for (Map map : mapList) {
            Map<String, Object> userAgeGroupMap = new HashMap<>();
            userAgeGroupMap.put("x", "".equals(((String)map.get("age_group"))) ? "未知" : map.get("age_group"));
            userAgeGroupMap.put("y", map.get("count"));
            res.add(userAgeGroupMap);
        }

        return res;
    }

    @Override
    public List<Map<String, Object>> selectOrderCountMap() {
        List<Map> mapList = orderWideMapper.selectOrderCountMap();
        List<Map<String, Object>> res = new ArrayList<>();

        String monthStr = new SimpleDateFormat("MM").format(new Date());
        String yearStr = new SimpleDateFormat("yyyy").format(new Date());

        int startMonth = 1;
        int endMonth = Integer.parseInt(monthStr);


        for (Map map : mapList) {
            int month = (int) map.get("month");
            if (month >= startMonth && month <= endMonth) {
                Map<String, Object> subMap = new HashMap<>();
                subMap.put("y", map.get("month") + "月");

                String countStr = ((BigInteger) map.get("count")).toString();
                int count = Integer.parseInt(countStr);
                subMap.put("x", count);

                String s = ((int) map.get("year")) == Integer.parseInt(yearStr) ? "s1" : "s2";
                subMap.put("s", s);
                res.add(subMap);
            }
        }
        return res;
    }

}
